package mo.org.cm309.shoppingcart.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import mo.org.cm309.shoppingcart.entity.Product;
import mo.org.cm309.shoppingcart.service.ProductService;
import mo.org.cm309.shoppingcart.utility.CommonUtility;

public class ProductDetailServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String id 		= req.getParameter("productId");
		long productId  = CommonUtility.getLongByString(id);
		ProductService productService = new ProductService();
		Product product 	= productService.getProductById(productId);
		
		if (product != null){
			req.setAttribute("product", product);
			RequestDispatcher dispatcher = req.getRequestDispatcher("productDetail.jsp");
			dispatcher.forward(req, resp);
		}else{
			PrintWriter writer = resp.getWriter();
			writer.print("<html>");
			writer.print("Error 404 : Product Not Found !");
			writer.print("</html>");
		}
	}
}
